package com.families.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.families.dao.AlbumMapper;
import com.families.dao.UserMapper;
import com.families.entity.Album;
import com.families.entity.Result;
import com.families.entity.User;
import com.families.entity.vo.UploadBackVo;
import com.families.service.AlbumService;
import com.families.util.ModelUtil;
import com.families.util.ObjectCheck;
@Service("albumService")
public class AlbumServiceImpl implements AlbumService{
	private static final Logger logger = LoggerFactory.getLogger(AlbumServiceImpl.class);
	private static final String VIEW_PATH="/album/showImage?id=";
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private UserMapper userMapper;
	
	public UploadBackVo insert(Album album) {
		// TODO Auto-generated method stub
		logger.info("--------------------进入上传模块---------------------------");
		ModelUtil.setCommonFields(album);
		logger.info("上传图片数据：{}",JSON.toJSONString(album));
		int insert = albumMapper.insert(album);
		if(insert<1) {
			logger.error("添加失败");
			return null;
		}
		UploadBackVo backVo=new UploadBackVo();
		backVo.setId(album.getId());
		backVo.setPath(album.getFilePath());
		backVo.setView(VIEW_PATH+album.getId());
		return backVo;
	}

    
	public List<UploadBackVo> findByUserId(String id) {
		// TODO Auto-generated method stub
		logger.info("-------------------------进入按照用户id查找图片信息模块---------------------------");
		
		List<Album> list= albumMapper.findByUserId(id);
		if(ObjectCheck.isEmpty(list)) {
			logger.error("未找到图片");
			return null;
		}
		List<UploadBackVo> backList=new ArrayList<UploadBackVo>();
		for (Album album : list) {
			UploadBackVo backVo=new UploadBackVo();
			backVo.setId(album.getId());
			backVo.setPath(album.getFilePath());
			backVo.setView(VIEW_PATH+album.getId());
			backList.add(backVo);
		}
		return backList;
	}


	
	public Album findById(String id) {
		// TODO Auto-generated method stub
		//logger.info("----------------------根据图片id查找--------------------------");
		Album album= albumMapper.findById(id);
		return album;
	}


	@Override
	public List<UploadBackVo> findAll() {
		// TODO Auto-generated method stub
		//logger.info("-------------------------------------------进入查找所有图片------------------------------");
		List<Album> list= albumMapper.findAll();
		if(ObjectCheck.isEmpty(list)) {
			logger.error("未找到图片");
			return null;
		}
		List<UploadBackVo> backList=new ArrayList<UploadBackVo>();
		for (Album album : list) {
			User user= userMapper.findById(album.getUid());
			UploadBackVo backVo=new UploadBackVo();
			backVo.setId(album.getId());
			backVo.setPath(album.getFilePath());
			backVo.setView(VIEW_PATH+album.getId());
			backVo.setUserName(user.getUserName());
			backList.add(backVo);
		}
		return backList;
	}

}
